package com.example.theorygame.util;

import org.cocos2d.actions.base.CCAction;
import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.layers.CCTMXObjectGroup;
import org.cocos2d.layers.CCTMXTiledMap;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCNode;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrame;
import org.cocos2d.transitions.CCFadeTransition;
import org.cocos2d.types.CGPoint;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 工具类
 * 1.动画效果（帧动画显示时长）animate
 * 2.动画效果（帧动画没有时长）animate
 * 3.图层切换changeLayer
 *
 * @author yangfan
 * path 图片路况
 * num 图片总数
 * repeat 动画是否重复
 * time 图片显示时长
 */
public class CommouUtils {

    //1
    public static CCAction animate(String path, int num, boolean repeat, float time) {
        //1.精灵图片列表
        ArrayList<CCSpriteFrame> frames = new ArrayList<CCSpriteFrame>();
        //2.初始化列表
        for (int i = 1; i <= num; i++) {
            //image/loading/loading_%02d.png 路径
            //String.format(path,i)
            frames.add(CCSprite.sprite(String.format(path, i)).displayedFrame());
        }
        //3.动画
        CCAnimation ani = CCAnimation.animation("loading", time, frames);
        //4.判断是否重复
        if (!repeat) {
            CCAnimate mt = CCAnimate.action(ani, false);
            return mt;
        } else {
            CCAnimate mt = CCAnimate.action(ani);
            CCRepeatForever r = CCRepeatForever.action(mt);
            return r;
        }
    }

    //2
    public static CCAction animate(String path, int num, boolean repeat) {
        return animate(path, num, repeat, 0.2f);
    }

    /**
     * 切换场景
     *
     * @param layer 下一个场景图层
     */
    public static void changeLayer(CCLayer layer) {
        // TODO Auto-generated method stub
        //场景
        CCScene scene = CCScene.node();
        //添加图层到场景中
        scene.addChild(layer);
        //淡入淡出处理
        CCFadeTransition ft = CCFadeTransition.transition(1, scene);
        //导演执行
//        CCDirector.sharedDirector().runWithScene(ft);
        CCDirector.sharedDirector().replaceScene(ft);
    }

    /**
     * 获取map_day.tmx文件
     *
     * @param map
     * @param groupName 组名
     * @return
     */
    public static ArrayList<CGPoint> loadPoint(CCTMXTiledMap map, String groupName) {
        //1.设置列表
        ArrayList<CGPoint> points = new ArrayList<CGPoint>();
        //2.获取数据
        CCTMXObjectGroup groups = map.objectGroupNamed(groupName);
        //3.根据分组信息获取内部数据
        ArrayList<HashMap<String, String>> objects = groups.objects;
        //4.循环迭代并存储到points
        for (HashMap<String, String> hashMap : objects) {
            //获取X,Y值
            float x = Integer.parseInt(hashMap.get("x"));
            float y = Integer.parseInt(hashMap.get("y"));
            //将其添加到points
            points.add(CCNode.ccp(x, y));
        }
        return points;
    }
}
